﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Util;
using Me.Amon.WMS.C;

namespace Me.Amon.OMS
{
    public partial class UpdateSpu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var cat = Request["cat"];
            if (!CharUtil.IsValidateHash(cat))
            {
                cat = "0";
            }
            var spu = Request["spu"];

            var dba = new RDBEngine();
            ListCat(dba, cat);

            dba.Reset();
            ReadSpu(dba, spu);
        }

        protected void TvCats_SelectedNodeChanged(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            //var dba = new DBAccess();
            //ListSpu(dba, node.Value);
        }

        #region 事件处理
        protected void BtUpdateSpu_Click(object sender, EventArgs e)
        {
            SaveSpu();
        }

        protected void BtDeleteSpu_Click(object sender, EventArgs e)
        {

        }

        protected void BtCancelSpu_Click(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            Response.Redirect("~/OMS/Spu.aspx?cat=" + node.Value);
        }
        #endregion

        #region 目录
        private void ListCat(RDBEngine dba, string cat)
        {
            var root = new TreeNode();
            root.Value = "0";
            root.Text = "商品类别";
            TvCats.Nodes.Add(root);
            root.Selected = (cat == root.Value);

            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.PID, "0");
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);
                node.Selected = (cat == node.Value);

                LoadCat(dba, node, cat);
            }
        }

        private void LoadCat(RDBEngine dba, TreeNode root, string cat)
        {
            dba.Reset();
            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.PID, root.Value);
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);
                node.Selected = (cat == node.Value);

                LoadCat(dba, node, cat);
            }
        }
        #endregion

        private void ReadSpu(RDBEngine dba, string spu)
        {
            dba.AddTable(DSpu.TABLE);
            dba.AddWhere(DSpu.ID, spu);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HdId.Value = spu;
            TbCode.Text = (string)row[DSpu.CODE];
            TbName.Text = (string)row[DSpu.NAME];
            TbUom.Text = (string)row[DSpu.UOM_ID];
            TbHypy.Text = (string)row[DSpu.HYPY];
            TbSpec.Text = (string)row[DSpu.SPEC];
            TbMode.Text = (string)row[DSpu.MODE];
            TbMaxInv.Text = "" + row[DSpu.MAX_INV];
            TbMinInv.Text = "" + row[DSpu.MIN_INV];
            TbVdY.Text = "" + row[DSpu.VALID_YEAR];
            TbVdM.Text = "" + row[DSpu.VALID_MONTH];
            TbVdd.Text = "" + row[DSpu.VALID_DAY];
        }

        private void SaveSpu()
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                LlErr.Text = "请选择商品分类！";
                return;
            }

            var name = TbName.Text.Trim();
            if (name.Length > 10)
            {
                LlErr.Text = string.Format("商品名称不能超过 {0} 个字符！", 10);
                return;
            }

            var vdy = int.Parse(TbVdY.Text);
            if (vdy < 0)
            {
                LlErr.Text = "有效期年不能小于0！";
                return;
            }

            var vdm = int.Parse(TbVdM.Text);
            if (vdm < 0)
            {
                LlErr.Text = "有效期月不能小于0！";
                return;
            }

            var vdd = int.Parse(TbVdd.Text);
            if (vdd < 0)
            {
                LlErr.Text = "有效期日不能小于0！";
                return;
            }

            var hypy = TbHypy.Text.Trim();

            var mode = TbMode.Text.Trim();
            var spec = TbSpec.Text.Trim();

            var id = HdId.Value;
            var user = WebUtil.CurrentUser(Session);

            var dba = new RDBEngine();

            bool isUpdate = CharUtil.IsValidateHash(id);
            string code = "";
            if (!isUpdate)
            {
                code = KeyUtil.NextSpuKey(dba, user.CorpId);
                dba.Reset();
            }

            dba.AddTable(DSpu.TABLE);
            dba.AddParam(DSpu.NAME, name);
            dba.AddParam(DSpu.HYPY, hypy);
            dba.AddParam(DSpu.MODE, mode);
            dba.AddParam(DSpu.SPEC, spec);
            dba.AddParam(DSpu.UOM_ID, "0");
            dba.AddParam(DSpu.CAT_ID, node.Value);
            dba.AddParam(DSpu.MERCHANT_ID, "");
            dba.AddParam(DSpu.MERCHANT_CODE, "");
            dba.AddParam(DSpu.MERCHANT_NAME, "");
            dba.AddParam(DSpu.MAX_INV, 0);
            dba.AddParam(DSpu.MIN_INV, 0);
            dba.AddParam(DSpu.VALID_YEAR, vdy);
            dba.AddParam(DSpu.VALID_MONTH, vdm);
            dba.AddParam(DSpu.VALID_DAY, vdd);
            dba.AddParam(DSpu.UPDATE_TIME, DSpu.NOW, false);
            dba.AddParam(DSpu.UPDATE_USER, user.UserId);
            if (isUpdate)
            {
                dba.AddWhere(DSpu.ID, id);
                dba.ExecuteUpdate();
            }
            else
            {
                id = HashUtil.UtcTimeInHex();
                dba.AddParam(DSpu.ID, id);
                dba.AddParam(DSpu.CODE, code);
                dba.AddParam(DSpu.CREATE_TIME, DSpu.NOW, false);
                dba.AddParam(DSpu.CREATE_USER, user.UserId);
                dba.ExecuteInsert();

                ShowDef();
            }
        }

        private void ShowDef()
        {
            HdId.Value = "";
            TbCode.Text = "";
            TbName.Text = "";
            TbHypy.Text = "";
            TbMode.Text = "";
            TbSpec.Text = "";
            TbMaxInv.Text = "0";
            TbMinInv.Text = "0";
            TbVdY.Text = "0";
            TbVdM.Text = "0";
            TbVdd.Text = "0";
            TbBars.Text = "";
            TbUom.Text = "";
        }
    }
}